/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.server;

import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Collections;
import org.internna.iwebjtracker.server.scm.SourceControlManagementServer;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import static org.internna.iwebmvc.utils.StringUtils.hasText;
import static org.internna.iwebmvc.utils.CollectionUtils.isNotEmpty;

/**
 * Handles all the servers in this context.
 *
 * @author Jose Noheda
 * @since 1.0
 */
@Service("serverManager")
public final class ServerManager {

    @Autowired private Set<Server<?, ?>> configuredServers;

    /**
     * Returns all the servers currently configured in the environment. Servers are loaded at startup
     * and won't be modified at a later time.
     */
    public Set<Server<?, ?>> getConfiguredServers() {
        return Collections.unmodifiableSet(configuredServers);
    }

    /**
     * Returns the types of all the configured servers.
     *
     * @return a non null non modifiable group of type of servers
     */
    public Set<String> getConfiguredServerTypes() {
        Set<String> types = new HashSet<String>();
        for (Server<?, ?> server : configuredServers) {
            types.add(server.getServerType());
        }
        return Collections.unmodifiableSet(types);
    }

    /**
     * Returns the type of all the configured Source Control Management servers.
     *
     * @return a non null non modifiable group of type of servers
     */
    public Set<String> getConfiguredSCMServers() {
        Set<String> types = new HashSet<String>();
        for (Server<?, ?> server : configuredServers) {
            if (server instanceof SourceControlManagementServer) {
                types.add(server.getServerType());
            }
        }
        return Collections.unmodifiableSet(types);
    }

    /**
     * Returns a server from the configured ones that can handle the provided server type.
     */
    public Server<?, ?> getConfiguredServerByType(String serverType) {
        Server<?, ?> found = null;
        if (hasText(serverType)) {
            for (Server<?, ?> server : configuredServers) {
                if (serverType.equals(server.getServerType())) {
                    found = server;
                }
            }
        }
        return found;
    }

    /**
     * Returns a server that is an instance of the parameter class
     *
     * @param <T> a server implementation
     * @param serverType the class or interface that the server must implement
     * @return null or one of the configured servers
     */
    @SuppressWarnings("unchecked")
    public <T extends Server<?, ?>> T getConfiguredServerByClass(Class<T> serverType) {
        T found = null;
        if (serverType != null) {
            for (Server<?, ?> server : configuredServers) {
                if (serverType.isInstance(server)) {
                    found = (T) server;
                }
            }
        }
        return found;
    }

    /**
     * Get all the supported protocols from all the configured servers grouped by server type.
     *
     * @return a map with server types as keys and a collection of protocols supported as values
     */
    public Map<String, Set<String>> getProtocols() {
        Map<String, Set<String>> protocols = null;
        if (isNotEmpty(configuredServers)) {
            protocols = new HashMap<String, Set<String>>(configuredServers.size());
            for (Server server : configuredServers) {
                protocols.put(server.getServerType(), server.getSupportedProtocols());
            }
        }
        return protocols;
    }

}
